﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
	/// <summary>
	/// 数据访问类:chkInfoStdLocal
	/// </summary>
	public partial class ChkInfoStdLocal
	{
		public ChkInfoStdLocal()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(decimal FYear,string FDeptNo,string FEntityNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_chkInfoStdLocal");
			strSql.Append(" where FYear=@FYear and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
			SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
			parameters[0].Value = FYear;
			parameters[1].Value = FDeptNo;
			parameters[2].Value = FEntityNo;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_chkInfoStdLocal(");
			strSql.Append("FYear,FDeptNo,FEntityNo,FBaseScore,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4)");
			strSql.Append(" values (");
			strSql.Append("@FYear,@FDeptNo,@FEntityNo,@FBaseScore,@FBaseScore1,@FBaseScore2,@FBaseScore3,@FBaseScore4)");
			SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBaseScore", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore1", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore2", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore3", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore4", SqlDbType.Decimal,9)};
			parameters[0].Value = model.FYear;
			parameters[1].Value = model.FDeptNo;
			parameters[2].Value = model.FEntityNo;
			parameters[3].Value = model.FBaseScore;
			parameters[4].Value = model.FBaseScore1;
			parameters[5].Value = model.FBaseScore2;
			parameters[6].Value = model.FBaseScore3;
			parameters[7].Value = model.FBaseScore4;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_chkInfoStdLocal set ");
			strSql.Append("FBaseScore=@FBaseScore,");
			strSql.Append("FBaseScore1=@FBaseScore1,");
			strSql.Append("FBaseScore2=@FBaseScore2,");
			strSql.Append("FBaseScore3=@FBaseScore3,");
			strSql.Append("FBaseScore4=@FBaseScore4");
			strSql.Append(" where FYear=@FYear and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
			SqlParameter[] parameters = {
					new SqlParameter("@FBaseScore", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore1", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore2", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore3", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore4", SqlDbType.Decimal,9),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
			parameters[0].Value = model.FBaseScore;
			parameters[1].Value = model.FBaseScore1;
			parameters[2].Value = model.FBaseScore2;
			parameters[3].Value = model.FBaseScore3;
			parameters[4].Value = model.FBaseScore4;
			parameters[5].Value = model.FYear;
			parameters[6].Value = model.FDeptNo;
			parameters[7].Value = model.FEntityNo;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(decimal FYear,string FDeptNo,string FEntityNo)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_chkInfoStdLocal ");
			strSql.Append(" where FYear=@FYear and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
			SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
			parameters[0].Value = FYear;
			parameters[1].Value = FDeptNo;
			parameters[2].Value = FEntityNo;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal GetModel(decimal FYear,string FDeptNo,string FEntityNo)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 FYear,FDeptNo,FEntityNo,FBaseScore,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4 from t_chkInfoStdLocal ");
			strSql.Append(" where FYear=@FYear and FDeptNo=@FDeptNo and FEntityNo=@FEntityNo ");
			SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
			parameters[0].Value = FYear;
			parameters[1].Value = FDeptNo;
			parameters[2].Value = FEntityNo;

			ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal model=new ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select FYear,FDeptNo,FEntityNo,FBaseScore,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4 ");
			strSql.Append(" FROM t_chkInfoStdLocal ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" FYear,FDeptNo,FEntityNo,FBaseScore,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4 ");
			strSql.Append(" FROM t_chkInfoStdLocal ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_chkInfoStdLocal ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FEntityNo desc");
			}
			strSql.Append(")AS Row, T.*  from t_chkInfoStdLocal T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_chkInfoStdLocal";
			parameters[1].Value = "FEntityNo";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod

		#region  已经使用的

        /// <summary>
        /// 获取基分定义
        /// <creator>sunlj</creator>
        /// <date>2013-10-17</date>
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decYear"></param>
        /// <returns></returns>
        public DataSet GetListByYear2(string strEntityNo, decimal decYear)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FDeptNo,FBaseScore,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4 ");
            strSql.Append(" FROM t_chkInfoStdLocal  where FEntityNo=@FEntityNo and FYear=@FYear");
            strSql.Append(" and (FDeptNo in(select FDeptNo from t_chkInfoStdLocal group by FDeptNo having sum(FBaseScore)>0)");
            strSql.Append(" or FDeptNo in(select FDeptNo from t_chkInfoStdSuper group by FDeptNo having sum(FBaseScore)>0))");
            SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = decYear;
            parameters[1].Value = strEntityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// <creator>sunlj</creator>
        /// <date>2013-10-17</date>
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal model = new ElingOA6_Admin_DSJCJ.Model.ChkInfoStdLocal();
            if (row != null)
            {
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FBaseScore"] != null && row["FBaseScore"].ToString() != "")
                {
                    model.FBaseScore = decimal.Parse(row["FBaseScore"].ToString());
                }
                if (row["FBaseScore1"] != null && row["FBaseScore1"].ToString() != "")
                {
                    model.FBaseScore1 = decimal.Parse(row["FBaseScore1"].ToString());
                }
                if (row["FBaseScore2"] != null && row["FBaseScore2"].ToString() != "")
                {
                    model.FBaseScore2 = decimal.Parse(row["FBaseScore2"].ToString());
                }
                if (row["FBaseScore3"] != null && row["FBaseScore3"].ToString() != "")
                {
                    model.FBaseScore3 = decimal.Parse(row["FBaseScore3"].ToString());
                }
                if (row["FBaseScore4"] != null && row["FBaseScore4"].ToString() != "")
                {
                    model.FBaseScore4 = decimal.Parse(row["FBaseScore4"].ToString());
                }
            }
            return model;
        }
        /// <summary>
        /// 获取所有的年份，按年份降序
        /// <creator>zhongyl</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public DataSet GetYeat(string strEntityNo)
        {
           StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT FYear FROM [dbo].[t_chkInfoStdSuper] GROUP BY FYear");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = strEntityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
             
        }


        public void addChkInfoLoaclAndSuper(List<Model.ChkInfoStdLocal> listChkInfoStdLocal, List<Model.ChkInfoStdSuper> listChkINfoStdSuper, string strEntityNo, decimal decYear)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();

            //删除区本级录用管理
            strSql.Append("delete from t_chkInfoStdLocal ");
            strSql.Append(" where FYear=@FYear  and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters2 = {
					new SqlParameter("@FYear", SqlDbType.Decimal),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters2[0].Value = decYear;
            parameters2[1].Value = strEntityNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);

            //删除上级录用管理
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("delete from t_chkInfoStdSuper ");
            strSql.Append(" where FYear=@FYear and FEntityNo=@FEntityNo");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FYear", SqlDbType.Decimal),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters3[0].Value = decYear;
            parameters3[1].Value = strEntityNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);

            //增加区局录用管理
            if (listChkInfoStdLocal.Count > 0)
            {
                foreach (Model.ChkInfoStdLocal model in listChkInfoStdLocal)
                {
                    strSql = new StringBuilder();
                    cmds = new CommandInfo();
                    strSql.Append("insert into t_chkInfoStdLocal(");
                    strSql.Append("FYear,FDeptNo,FEntityNo,FBaseScore,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4)");
                    strSql.Append(" values (");
                    strSql.Append("@FYear,@FDeptNo,@FEntityNo,@FBaseScore,@FBaseScore1,@FBaseScore2,@FBaseScore3,@FBaseScore4)");
                    SqlParameter[] parameters = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBaseScore", SqlDbType.Decimal,9),
                    new SqlParameter("@FBaseScore1", SqlDbType.Decimal,9),
                    new SqlParameter("@FBaseScore2", SqlDbType.Decimal,9),
                    new SqlParameter("@FBaseScore3", SqlDbType.Decimal,9),
                    new SqlParameter("@FBaseScore4", SqlDbType.Decimal,9)
                                                };
                    parameters[0].Value = model.FYear;
                    parameters[1].Value = model.FDeptNo;
                    parameters[2].Value = model.FEntityNo;
                    parameters[3].Value = model.FBaseScore;
                    parameters[4].Value = model.FBaseScore1;
                    parameters[5].Value = model.FBaseScore2;
                    parameters[6].Value = model.FBaseScore3;
                    parameters[7].Value = model.FBaseScore4;
                    cmds.CommandText = strSql.ToString();
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);
                }
            }

            if (listChkINfoStdSuper.Count > 0)
            {
                foreach (Model.ChkInfoStdSuper model in listChkINfoStdSuper)
                {
                    strSql = new StringBuilder();
                    cmds = new CommandInfo();
                    strSql.Append("insert into t_chkInfoStdSuper(");
                    strSql.Append("FYear,FBaseScore,FDeptNo,FEntityNo,FInfoClsNo,FBaseScore1,FBaseScore2,FBaseScore3,FBaseScore4)");
                    strSql.Append(" values (");
                    strSql.Append("@FYear,@FBaseScore,@FDeptNo,@FEntityNo,@FInfoClsNo,@FBaseScore1,@FBaseScore2,@FBaseScore3,@FBaseScore4)");
                    SqlParameter[] parameters1 = {
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FBaseScore", SqlDbType.Decimal,9),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInfoClsNo", SqlDbType.NVarChar,20),
					new SqlParameter("@FBaseScore1", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore2", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore3", SqlDbType.Decimal,9),
					new SqlParameter("@FBaseScore4", SqlDbType.Decimal,9)};
                    parameters1[0].Value = model.FYear;
                    parameters1[1].Value = model.FBaseScore;
                    parameters1[2].Value = model.FDeptNo;
                    parameters1[3].Value = model.FEntityNo;
                    parameters1[4].Value = model.FInfoClsNo;
                    parameters1[5].Value = model.FBaseScore1;
                    parameters1[6].Value = model.FBaseScore2;
                    parameters1[7].Value = model.FBaseScore3;
                    parameters1[8].Value = model.FBaseScore4;

                    cmds.CommandText = strSql.ToString();
                    cmds.Parameters = parameters1;
                    cmdList.Add(cmds);
                }
            }

            DbHelperSQL.ExecuteSqlTranWithIndentity(cmdList);


        }

		#endregion 已经使用的
	}
}

